import { FC, useEffect } from "react"
import { useRoutes, useNavigate, useLocation } from "react-router-dom"
import useStorage from "./useStorage"

interface Iprops {
    routes: RouteObj[]; //routes配置
    tokenKey: string //token的key
}
const useRouteWithGuard: FC<Iprops> = (props:Iprops) => {
    let { routes, tokenKey } = props;
    let [token, methods] = useStorage('tq-token')
    // console.log(props)
    // console.log(methods)
    // console.log(tokenKey)
    //获取当前访问的路径
    let location = useLocation();
    // console.log(location)
    let navigate = useNavigate()
    //判断有没有token
    useEffect(() => {
        if (!token) {
            if (location.pathname !== '/login') {
                //跳转到登入页
                navigate('/login')
            }
        }
    },[location])
    let element = useRoutes(routes)
    return element
}

export default useRouteWithGuard;